{% template "admin/header.html" %}
<style>
    .layui-table-cell {
        font-size: 13px;
        color: #333;
    }
</style>
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>消息列表</legend>
    </fieldset>
    <div class="demoTable">
        <select id="keys" style="height: 35px;padding: 0 5px;">
            <option value="title">标题</option>
            <option value="tel">手机号</option>
            <option value="ip">IP</option>
            <option value="name">名称</option>
        </select>
        <div class="layui-inline">
            <input class="layui-input" name="key" id="values" placeholder="请输入关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

{% template "admin/footer.html" %}
<script type="text/html" id="action">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="tim">
    {{ formatDate(d.Addtime) }}
</script>

<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'message',
            elem: '#list',
            url: '/admin/message/query',
            where:{},
            method: 'post',
            page: true,
            cols: [[
                /*{checkbox: true},*/
                {field: 'Message_id', title: 'ID', width: 80},
                {field: 'Name', title: '名称', width: 180},
                {field: 'Email', title: 'email', width: 150},
                {field: 'Tel', title: '电话', width: 150},
                {field: 'Content', title: '内容', width: 300},
                {field: 'Addtime', title: '留言时间', width: 150,toolbar: '#tim'},
                {field: 'Ip', title: 'IP', width: 150},
                {align: 'center', toolbar: '#action'}
            ]],
            limit: 10 //每页默认显示的数量
        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#keys').val();
            var values = $("#values").val();
            if ($.trim(values) === '') {
                layer.msg('请输入关键字！', {icon: 0});
                return;
            }
            var p={};
            p[key] = values;
            tableIn.reload({ page: {page: 1}, where: p});
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('您确定要删除该留言吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("/admin/message/delete",{id:data.Message_id},function(res){
                        layer.close(loading);
                        if(res.Code===1){
                            layer.msg(res.Msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });

    /**
     * 时间戳转换日期
     * @param now
     * @returns {string}
     */
    function formatDate(time) {
        time = parseInt(time);
        time = (time * 1000) + 8 * 3600 * 1000;
        var date = new Date(time);
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-');
    }

</script>
</body>
</html>
